package com.xiangyueEducation.uploaderCloud.init;

import com.xiangyueEducation.uploaderCloud.mq.u2uChat.CreateQueueUtils;
import com.xiangyueEducation.uploaderCloud.utils.enums.MQEnums;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Slf4j
public class MQ {

    @Autowired
    RabbitAdmin rabbitAdmin;
    @Autowired
    CreateQueueUtils createQueueUtils;

    @Resource(name = "batchImportExchange")
    DirectExchange directExchange;



    @PostConstruct
    public void init(){
        if (!createQueueUtils.hasQueue("batchImport-queue")){
            Queue queue = new Queue("batchImport-queue");
            rabbitAdmin.declareQueue(queue);
            Binding bind = BindingBuilder.bind(queue).to(directExchange).with(MQEnums.BATCH_IMPORT.getname());
            rabbitAdmin.declareBinding(bind);
            log.info("创建队列{}并绑定成功", "batchImport-queue");
        }else {
            log.info("队列{}已存在,不声明", "batchImport-queue");
        }
    }
}
